package com.it.dao.impl;

import com.it.Bean.sys_user;
import com.it.dao.Sys_userDao;
import com.it.db.DBManager;
import com.it.db.JdbcTemplate;
import com.it.mapping.EntityMapping;
import com.it.mapping.MappingFactory;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class Sys_userDaoimpl implements Sys_userDao {

    public static DBManager dbManager=null;
    public static Connection conn =null;
    public static JdbcTemplate jdbcTemplate=null;
    public static MappingFactory mappingFactory=null;
    public static EntityMapping map=null;
    static{
        //抽取数据库连接信息
        dbManager = DBManager.getInstance();
        //获得数据库连接
        conn = dbManager.getConnection();
        //获取JDBC工具类
        jdbcTemplate = JdbcTemplate.getInstance();
        //设置连接
        jdbcTemplate.setConnection(conn);
        //获取封装类工厂
        mappingFactory = MappingFactory.getInstance();
        //获取封装类
        map = mappingFactory.getMap(MappingFactory.SYS_USER_MAPPING);
    }
    @Override
    public sys_user queryById(Integer id) {
        List<Object> query = jdbcTemplate.query("select * from sys_user where id=?", map,id);
        if (query.size()!=0){
            sys_user suser = (sys_user) query.get(0);
            return suser;
        }

        return null;
    }

    @Override
    public List<sys_user> getQueryAll() {
        List<Object> query = jdbcTemplate.query("select * from sys_user ", map);
        List<sys_user> list = new ArrayList<>();
        for (Object qu : query ){
            sys_user suser = (sys_user) qu;
            list.add(suser);
        }
        return list;
    }

    @Override
    public boolean save(sys_user user) {
        int update = jdbcTemplate.update("insert into sys_user(name,sex,age,marryed,ad,tel) values(?,?,?,?,?,?)",
                user.getName(), user.getSex(), user.getAge(), user.getMarryed(), user.getAd(), user.getTel());
        if (update==1){
            return true;
        }else
            return false;
    }

    @Override
    public boolean update(sys_user user) {
        int update = jdbcTemplate.update("update sys_user set name=?,sex=?,age=?,marryed=?,ad=?,tel=? where id=?",
                user.getName(), user.getSex(), user.getAge(), user.getMarryed(), user.getAd(), user.getTel()
                , user.getId());
        if (update==1){
            return true;
        }else
            return false;
    }

    @Override
    public boolean delete(Integer id) {
        int update = jdbcTemplate.update("delete from sys_user where id=?");
        if (update==1){
            return true;
        }else
            return false;
    }
}
